<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $categoryId   = "";
  $categoryName = "";
  // after post : start 
  if(isset($_POST['itemName']))
  {
    $insertItem = "INSERT INTO item (categoryId,subCategoryId,itemName,currentStock,maxLevel,minLevel)
                    VALUE (".$_POST['categoryId'].",".$_POST['subCategoryId'].",'".$_POST['itemName']."',
                           ".$_POST['currentStock'].",".$_POST['maxLevel'].",".$_POST['minLevel'].")";
    $insertItemResult = mysql_query($insertItem);
    if(!$insertItemResult)
      die("Insert Query Not Inserted : ".mysql_error());
    else
    {
      echo $itemSelected = mysql_insert_id();	
      exit();
      //header("Location:item.php?categoryId=$categoryId");
    }
//    else
//      header("Location:item.php");
  }    
  // after post : end  

	////Select category : Start
  $selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                         WHERE categoryId IN (SELECT categoryId
	                                                FROM subcategory 
	                                               WHERE subCategoryId = '".$_REQUEST['subCategoryId']."')
	                      ORDER BY categoryName";
                          
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	if($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  $categorySelected      = $categoryRow['categoryId'];
	
	  $categoryId   = $categoryRow['categoryId'];
	  $categoryName = $categoryRow['categoryName'];
	}
	////Select category : End
  ////Select subcategory : Start
  $selectSubCategoryQuery = "SELECT subCategoryId,subCategoryName
	                             FROM subcategory
	                            WHERE subCategoryId = ".$_REQUEST['subCategoryId'];
  $selectSubQuery = mysql_query($selectSubCategoryQuery);
  if($selectCategoryRow = mysql_fetch_array($selectSubQuery))
	{
	  $subCategorySelected      = $selectCategoryRow['subCategoryId'];
	
	  $subCategoryId   = $selectCategoryRow['subCategoryId'];
	  $subCategoryName = $selectCategoryRow['subCategoryName'];
	}	                           
  
  ////Select subcategory : End 
  include("./bottom.php");
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("subCategorySelected",$subCategorySelected);
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryName",$categoryName);
  $smarty->assign("subCategoryId",$subCategoryId);
  $smarty->assign("subCategoryName",$subCategoryName);
  $smarty->display("itemaddAj.tpl");
}
?>